package com.wms.controller.admin;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.wms.entity.Admin;
import com.wms.entity.MyPage;
import com.wms.entity.Role;
import com.wms.service.AdminService;
import com.wms.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/admin/admin")
@RestController("adminAdminController")
public class AdminController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @RequestMapping("/list")
    public Object list(String username, MyPage<Admin> page){

        OrderItem orderItem = new OrderItem();
        orderItem.setAsc(false);
        orderItem.setColumn("id");
        page.getOrders().add(orderItem);

        if (StringUtils.hasText(username)){
            page.getCondition().put("username", username);
        }

        adminService.queryRolePage(page);
        return success(page);
    }

    @RequestMapping("/save")
    public Object save(Admin admin) {
        if (StringUtils.isEmpty(admin.getUsername()) || StringUtils.isEmpty(admin.getPassword())){
            return error("参数不完整");
        }

        Admin ad = adminService.findByUsername(admin.getUsername());
        if (ad != null){
            return error("用户名已存在");
        }

        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.save(admin);
        return success();
    }

    @RequestMapping("/detail")
    public Object detail(Long id) {
        if (id == null){
            return error("参数不完整");
        }

        Admin admin = adminService.getById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", admin.getId());
        map.put("username", admin.getUsername());
        map.put("createDate", admin.getCreateDate());
        return success(map);
    }

    @RequestMapping("/checkUsername")
    public Object checkUsername(String username){
        if (StringUtils.isEmpty(username)) return error("参数不完整");

        Admin admin = adminService.findByUsername(username);
        return success(admin == null);
    }

    @RequestMapping("/update")
    public Object update(Admin admin) {
        if (admin.getId() == null){
            return error("参数不完整");
        }

        if (StringUtils.hasText(admin.getPassword())){
            admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        }

        adminService.updateById(admin);
        return success();
    }

    @RequestMapping("/delete")
    public Object delete(Long id){
        if (id == null){
            return error("参数不完整");
        }

        adminService.deleteById(id);
        return success();
    }

    @RequestMapping("/deleteBatch")
    public Object deleteBatch(@RequestParam(value="ids[]") List<Long> ids){
        if (ids == null || ids.size() == 0){
            return error("参数不完整");
        }

        adminService.deleteByIds(ids);
        return success();
    }

    @RequestMapping("/bindRole")
    public Object bindRole(Long id, Long roleId) {
        if (id == null || roleId == null){
            return error("参数不完整");
        }

        Admin admin = adminService.getById(id);
        if (admin == null){
            return error("数据不存在");
        }

        Role role = roleService.getById(roleId);
        if (role == null){
            return error("数据不存在");
        }

        adminService.bindRole(admin, role);
        return success();
    }


}
